/*
 * Utiles_DB.java
 *
 * Created on 22 de julio de 2007, 07:12 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package utiles;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JComboBox;
import java.util.*;
/**
 *
 * @author Frank
 */
public class Utiles_DB {
    
    /** Creates a new instance of Utiles_DB */
    public Utiles_DB() {
    }
    
    public static void llenaComboString( ResultSet res, JComboBox cmb, int col ){
        try{
            res.beforeFirst();
            while( res.next() ){
                cmb.addItem( res.getString( col ) );
            }
        }catch( Exception er ){
            er.printStackTrace();
        }
    }
    
    public static void llenaComboInt( ResultSet res, JComboBox cmb, int col ){
        try{
            res.beforeFirst();
            while( res.next() ){
                cmb.addItem( "" + res.getInt( col ) );
            }
        }catch( SQLException er ){
            er.printStackTrace();
        }
    }
    
    public static ArrayList getArrayList( ResultSet res, int col ){
        ArrayList arlst = new ArrayList(1);
        try{
            res.beforeFirst();
            while( res.next() ){
                arlst.add( res.getString( col ) );
            }
        }catch( SQLException er ){
            er.printStackTrace();
        }
        return arlst;
    }
    
    public static ArrayList getArrayList( ResultSet res, int[] col ){
        ArrayList arlst = new ArrayList(1);
        String temp = null;
        try{
            res.beforeFirst();            
            while( res.next() ){
                temp = "";
                for( int i = 0; i < col.length; i++ ){
                    temp = temp + " " + res.getString( col[i] ).trim();
                }   
                arlst.add( temp.trim() );   //Agregamos las columnas
            }            
        }catch( Exception er ){
            er.printStackTrace();
        }
        return arlst;
    }
    
    public static ArrayList getArrayList( ArrayList res, int[] col ){
        ArrayList arlst = new ArrayList(1);
        ArrayList tempArr = null;
        String temp = null;        
        int[] posiciones = Utiles_Varios.clonarArreglo( col );
        Utiles_Ordenar.ordenar( posiciones );
        for( int i = 0; i < res.size() - 2; i++ ){
            tempArr = ( ArrayList ) res.get(i);
            temp = "";            
            //System.out.println( "Columnas-->" + col.length );            
            for( int j = 0; j < col.length; j++ ){                
                //System.out.println( col[j] + "  " + posiciones[ j ] );
                temp = temp + " " + ( tempArr.get( col[j] - 1 ).toString() ).trim();                           
            }
            for( int j = 0, cont = 0; j < col.length; j++ ){
                tempArr.remove( posiciones[ j ] - cont - 1 );
                cont++;
            }        
            arlst.add( temp.trim() );              
        }
        return arlst;
    }               
}
